package com.sos.controller;

import com.sos.entity.LayuiR;
import com.sos.entity.PieData;
import com.sos.entity.po.Account;
import com.sos.entity.po.Doctor;
import com.sos.service.AccountService;
import com.sos.service.DoctorService;
import com.sos.util.TokenUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 港某人
 * @date 2021/3/19 16:12
 */
@RestController
@RequestMapping(value = "doctor")
public class DoctorController {

    @Resource
    AccountService accountService;

    @Resource
    DoctorService doctorService;

    @RequestMapping(value = "login", method = {RequestMethod.POST})
    LayuiR<Boolean> login(Account account, HttpServletRequest request, HttpServletResponse response) {
        return accountService.login(account);
    }

    @RequestMapping(value = "register", method = {RequestMethod.POST})
    LayuiR<Boolean> register(Account account) {
        return accountService.register(account);
    }

    @RequestMapping(value = "info", method = {RequestMethod.POST})
    LayuiR<Doctor> getInfo(HttpServletRequest request) {
        String token = request.getHeader("x-token");
        return doctorService.info(token);
    }

    @RequestMapping(value = "update", method = {RequestMethod.POST})
    void updateInfo(Doctor doctor) {
        doctorService.updateInfo(doctor);
    }

    @RequestMapping(value = "updatePassword", method = {RequestMethod.POST})
    void updatePassword(String password1, String password2) {
        String doctorAccount = TokenUtil.getDoctorAccount();
        if (StringUtils.isNotBlank(doctorAccount)) {
            if (StringUtils.isNotBlank(password1) && StringUtils.isNotBlank(password2) && password1.equals(password2)) {
                Account account = new Account();
                account.setAccount(doctorAccount);
                account.setPassword(password1);
                doctorService.updatePassword(account);
            }
        }
    }

    @RequestMapping(value = "getPie1", method = {RequestMethod.GET})
    List<PieData> getPie1() {
        return doctorService.getPie1();
    }

    @RequestMapping(value = "getPie2", method = {RequestMethod.GET})
    List<PieData> getPie2() {
        return doctorService.getPie2();
    }

    @RequestMapping(value = "getBar1", method = {RequestMethod.GET})
    List<PieData> getBar1() {
        return doctorService.getBar1();
    }


}
